﻿/**
  * jQuery webcam plugin
  * Version 1.0
  * @requires jQuery v1.9.1 or later
  * Copyright (c) 2017-2019 Open
  * Data: 2018-5-5 
 */

; (function (factory) {
    if (typeof define === 'function' && define.amd) {
        // AMD
        define(['jquery', 'webcam'], factory);
    } else if (typeof exports === 'object') {
        // Node / CommonJS
        factory(require('jquery', 'webcam'));
    } else {
        // Globals
        factory(jQuery, Webcam);
    }
}(function ($, webcam) {
    $.fn.openWebCam = function (options) {
        //初始化摄像头
        var init = function (eleId, opts) {
            //调用摄像头
            webcam.set({
                height: opts.height, //相机视图的高度，默认是Dom元素的高度
                image_format: 'jpeg',
                jpeg_quality: 90
            });
            webcam.attach('#' + eleId);
        }
        var defaults = {
            height: 230
        }

        var opts = $.extend({}, defaults, options);

        return this.each(function () {
            var $this = $(this);
            init($this.attr('id'), opts);
        })
    };
    $.fn.openGetCamerPic = function (options) {
        //拍摄照片并且展示
        return this.each(function () {
            var $this = $(this);
            var picWidth = $this.width();
            var picHeight = $this.height();
            $this.find('.caminner').remove();
            webcam.snap(function (data_uri) {
                $this.append('<div class="caminner" style="position:absolute;left:0px;top:0px"><img style="width:' + picWidth + 'px;height:' + picHeight + 'px" src="' + data_uri + '"></div>');
                options.cb(data_uri);
            });
        })
    };
    $.fn.removeCamerPic = function () {
        //重新渲染
        return this.each(function () {
            var $this = $(this);
            $this.find('.caminner img').remove();
        })
    };

}));


